//
//  LandingRecordViewController.m
//  ChineseCollegeStudentsNetwork
//
//  Created by 刘喆 on 2018/5/14.
//  Copyright © 2018年 刘喆. All rights reserved.
//

#import "LandingRecordViewController.h"
#import "LandingRecordTableViewCell.h"
#import "RecordListModel.h"

static NSString *const LandingRecordTableViewCell_ID = @"LandingRecordViewCell_ID";

@interface LandingRecordViewController ()<UITableViewDataSource, UITableViewDelegate>

/** 上方提示页*/
@property (nonatomic, strong) UIView *promptView;
@property (nonatomic, strong) UILabel *promptL;
@property (nonatomic, strong) UILabel *modifyPassword;

/** 下方列表页*/
@property (nonatomic, strong) UITableView *tableView;//tableView
@property (nonatomic, strong) NSMutableArray *listArr;

@property (nonatomic, copy) NSString *last;
@property (nonatomic, copy) NSString *next;

@end

@implementation LandingRecordViewController

#pragma mark - Life Cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (_titleStr) {
        self.title = @"敏感记录";
    } else {
        self.title = @"登录记录";
    }
    
    [self loadData];
 
    
    [self setupConstraints];
    
    
}

- (void)loadData {
    
    NSString *api = [NSString stringWithFormat:@"/api/v1/users/%@/logs",USER_ID];
    
    [MLAHttpManage postHttpType:MLAHttpRequestType_Get hostType:MLAHttpHostType_Api api:api params:nil success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        _listArr = [NSMutableArray array];
        _listArr = [RecordListModel mj_objectArrayWithKeyValuesArray:response[@"value"]];
        _next = response[@"next"];
        _last = response[@"last"];
        
        if (![_next isEqualToString:_last]) {
            _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
        }
        [self.tableView reloadData];

    } failure:^{
        
    }];
    
}

- (void)loadMore {

    [MLAHttpManage postHttpType:MLAHttpRequestType_Get hostType:MLAHttpHostType_Api api:_next params:nil success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        [_listArr addObjectsFromArray:[RecordListModel mj_objectArrayWithKeyValuesArray:response[@"value"]]];
        _next = response[@"next"];
        _last = response[@"last"];
        
        if ([_next isEqualToString:_last]) {
            [_tableView.mj_footer endRefreshingWithNoMoreData];
        } else {
            [_tableView.mj_footer endRefreshing];
        }
        
        [self.tableView reloadData];
        
    } failure:^{
        
    }];
    
}


#pragma mark - Constraints
- (void)setupConstraints {
    
    [self.promptView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        make.height.mas_equalTo(@150);
    }];
    

    [self.promptL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.centerX.equalTo(self.promptView);
        make.height.mas_equalTo(@30);
    }];
    
    [self.modifyPassword mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(65);
        make.centerX.equalTo(self.view);
        make.left.equalTo(self.view).offset(50);
        make.right.equalTo(self.view).offset(-50);
        make.height.mas_equalTo(@50);
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(150);
        make.left.right.equalTo(self.view);
        make.bottom.mas_equalTo(0);
    }];
    
}
- (UIView *) promptView {
    if (!_promptView) {
        _promptView = [[UIView alloc] init];
        _promptView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_promptView];

    }
    return _promptView;
}


-(UILabel *) promptL {
    if (!_promptL) {
        _promptL = [[UILabel alloc] init];
        if (_titleStr) {
            _promptL.text = @"您的账号近期没有敏感操作";
        } else {
            _promptL.text = @"您的账号最近没有异常登陆";
        }

        [_promptView addSubview:_promptL];
    }
    return _promptL;
}
-(UILabel *)modifyPassword {
    if (!_modifyPassword) {
        _modifyPassword = [[UILabel alloc] init];
        if (_titleStr) {
            _modifyPassword.text = @"敏感操作是指修改密码,设置手机或邮箱,新增/编辑/删除稿件等可能影响到您账号安全的操作;如确定非您本人操作,建议您修改密码";
        } else {
            _modifyPassword.text = @"请您通过登录时间判断是否是您本人操作,如确定非本人,建议您修改密码";
        }

        _modifyPassword.numberOfLines = 0;
        _modifyPassword.font = [UIFont systemFontOfSize:12];
        _modifyPassword.textAlignment = NSTextAlignmentCenter;
        [_promptView addSubview:_modifyPassword];
    }
    return _modifyPassword;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.backgroundColor = UIColor.whiteColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedRowHeight = 100;
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        [_tableView registerClass:[LandingRecordTableViewCell class] forCellReuseIdentifier:LandingRecordTableViewCell_ID];
        [self.view addSubview:_tableView];
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
    }
    return _tableView;
}

#pragma mark -
#pragma mark tableview datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _listArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LandingRecordTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:LandingRecordTableViewCell_ID forIndexPath:indexPath];
    RecordListModel *model = _listArr[indexPath.row];
    cell.dateL.text = model.addDate;
    cell.timeL.text = model.action;
    cell.IpaddressL.text = model.ipAddress;
    return cell;
}

#pragma mark tableview delegate




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
